package com.example.hello;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.*;

import java.time.LocalTime;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    TextView count;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bmi);
         //showout = (EditText)findViewById(R.id.tem);
       // showout.setText("data:2001.04.07");
      //out = findViewById(R.id.show);
        Button btn = (Button)findViewById(R.id.btn);
       btn.setOnClickListener(this);

    }

    @Override
    public  void onClick(View v) {
        //获取用户输入
        EditText editHeight = (EditText)findViewById(R.id.high);
        EditText editWeidth = (EditText)findViewById(R.id.weight);
        TextView txtResault = (TextView)findViewById(R.id.show);
        //获取编辑框内容，由于是字符串类型，需要转换为可计算类型
        Double height = Double.parseDouble(editHeight.getText().toString());
        Double weight = Double.parseDouble(editWeidth.getText().toString());
        DecimalFormat df=new DecimalFormat(".##");
        //设置判断语句
        Double bmi = weight / (height*height);
        String st=df.format(bmi);
        if(bmi<18.5){
            txtResault.setText("BMI"+st.toString()+",您的体重偏轻，请注意饮食");
        }
        else  if(bmi<=24.9){
            txtResault.setText("BMI"+st.toString()+",您的体重正常，继续保持");
        }
        else if(bmi<=29.9){
            txtResault.setText("BMI"+st.toString()+",您的体重偏重，请注意饮食");
        }
        else if (bmi<=34.9){
            txtResault.setText("BMI"+st.toString()+",您的体重肥胖!请注意平时饮食摄入");
        }
        else if (bmi<=39.9){
            txtResault.setText("BMI"+st.toString()+",您的体重过于肥胖!必要时请寻求医生帮助");
        }


    }
}